﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Yipusoft.Account.BLL;
using Yipusoft.Account.Model;
using Yipusoft.Common.BLL;
using Yipusoft.Common.Model;
using Yipusoft.Task.BLL;
using Yipusoft.Task.Model;
using Yipusoft.Evaluate.BLL;
using Yipusoft.Evaluate.Model;
using System.Text;
using System.Data;

public partial class Evaluate_AllDetail : System.Web.UI.Page
{
    private int dcount;
    private int tlength = 10;
    private int row = 1;
    private decimal temp = 0;
    private decimal[] total = new decimal[100];
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindYearList();
            BindStartMonthList();
            BindEndMonthList();
            if (Request["year"]!=null)
            {
                YearList.SelectedValue = Request["year"];
                StartMonthList.SelectedValue = Request["sm"];
                EndMonthList.SelectedValue = Request["em"];
            }
            else
            {
                YearList.SelectedValue = DateTime.Now.Year.ToString();
                StartMonthList.SelectedValue = DateTime.Now.Month.ToString();
                EndMonthList.SelectedValue = DateTime.Now.Month.ToString();
            }
        }
        CreateTable();

    }


    private string GetDepartmentID()
    {
        string DepartmentID = Yipusoft.Account.BLL.User.GetUser(User.Identity.Name).DepartmentID;
        string ReturnStr = string.Empty;

        List<RoleInfo> list = Yipusoft.Account.BLL.User.GetRoleList(Yipusoft.Account.BLL.User.GetUser(User.Identity.Name).UserID);

        bool Flag = list.Where(o => o.RoleID.Equals("100110")).Count() > 0;
        if (Flag)
        {
            return "";
        }
        if (!DepartmentID.Substring(4, 1).Equals("0"))
            return "";

        return " Right(a.DepartmentID,3)=''0" + DepartmentID.Substring(5, 1) + "0'' And ";

    }

    private string GetDepartmentID2()
    {
        string DepartmentID = Yipusoft.Account.BLL.User.GetUser(User.Identity.Name).DepartmentID;
        string ReturnStr = string.Empty;

        List<RoleInfo> list = Yipusoft.Account.BLL.User.GetRoleList(Yipusoft.Account.BLL.User.GetUser(User.Identity.Name).UserID);

        bool Flag = list.Where(o => o.RoleID.Equals("100110")).Count() > 0;
        if (Flag)
        {
            return "";
        }
        if (!DepartmentID.Substring(4, 1).Equals("0"))
            return "";

        return " Right(a.DepartmentID,3)='0" + DepartmentID.Substring(5, 1) + "0' And ";

    }

    private string GetDepartmentID3()
    {
        string DepartmentID = Yipusoft.Account.BLL.User.GetUser(User.Identity.Name).DepartmentID;
        string ReturnStr = string.Empty;

        List<RoleInfo> list = Yipusoft.Account.BLL.User.GetRoleList(Yipusoft.Account.BLL.User.GetUser(User.Identity.Name).UserID);

        bool Flag = list.Where(o => o.RoleID.Equals("100110")).Count() > 0;
        if (Flag)
        {
            return "";
        }
        if (!DepartmentID.Substring(4, 1).Equals("0"))
            return "";

        return " And Right(a.DepartmentID,3)=''0" + DepartmentID.Substring(5, 1) + "0''  ";

    }

    private string GetDepartmentIDChar()
    {
        string DepartmentID = Yipusoft.Account.BLL.User.GetUser(User.Identity.Name).DepartmentID;
        string ReturnStr = string.Empty;

        List<RoleInfo> list = Yipusoft.Account.BLL.User.GetRoleList(Yipusoft.Account.BLL.User.GetUser(User.Identity.Name).UserID);

        bool Flag = list.Where(o => o.RoleID.Equals("100110")).Count() > 0;
        if (Flag)
        {
            return "";
        }
        if (!DepartmentID.Substring(4, 1).Equals("0"))
            return "";

        return " Where  Right(DepartmentID,3)='0" + DepartmentID.Substring(5, 1) + "0' and flag=0 ";

    }
    private void BindYearList()
    {
        for (int i = 2008; i <= 2050; i++)
        {
            ListItem li = new ListItem();
            li.Text = i.ToString() + "年";
            li.Value = i.ToString();
            YearList.Items.Add(li);
        }
        YearList.SelectedValue = DateTime.Now.Year.ToString();
    }
    private void BindStartMonthList()
    {
        for (int i = 1; i <= 12; i++)
        {
            ListItem li = new ListItem();
            li.Text = i.ToString() + "月";
            li.Value = i.ToString();
            StartMonthList.Items.Add(li);
        }
        StartMonthList.SelectedIndex = StartMonthList.Items.IndexOf(StartMonthList.Items.FindByValue(DateTime.Now.Month.ToString()));
    }
    private void BindEndMonthList()
    {
        for (int i = 1; i <= 12; i++)
        {
            ListItem li = new ListItem();
            li.Text = i.ToString() + "月";
            li.Value = i.ToString();
            EndMonthList.Items.Add(li);
        }
        EndMonthList.SelectedIndex = EndMonthList.Items.IndexOf(EndMonthList.Items.FindByValue(DateTime.Now.Month.ToString()));
    }
    private void CreateTable()
    {
        for (int i = 0; i < dcount; i++)
        {
            total[i] = 0;
        }
        DataView dv = SplitPage.ExecuteSQL("SELECT DepartmentID, AliasName FROM Account_Departments WHERE SUBSTRING(DepartmentId,3,2) <> '00' AND SUBSTRING(DepartmentId,5,3) = '000' and left(DepartmentID,4)<>'1314' and left(DepartmentID,4)<>'1315'").Tables[0].DefaultView;
        dcount = dv.Count;
        TableRow tr = new TableRow();
        tr.CssClass = "grid_header";
        tr.HorizontalAlign = HorizontalAlign.Right;
        //月份
        TableCell tc = new TableCell();
        tc.CssClass = "Td";
        tc.Text = "月份";
        tc.Width = Unit.Pixel(30);
        tr.Cells.Add(tc);
        //多选框
        tc = new TableCell();
        tc.CssClass = "Td";
        tc.Width = Unit.Pixel(1);
        tc.HorizontalAlign = HorizontalAlign.Center;
        CheckBox cbx = new CheckBox();
        cbx.Attributes.Add("onclick", "CheckAll(this)");
        tc.Controls.Add(cbx);
        tr.Cells.Add(tc);


        tc = new TableCell();
        tc.CssClass = "Td";
        tc.HorizontalAlign = HorizontalAlign.Center;
        tc.Text = "项目";
        tr.Cells.Add(tc);
        foreach (DataRowView drv in dv)
        {
            tc = new TableCell();
            tc.CssClass = "Td";
            tc.HorizontalAlign = HorizontalAlign.Right;
            tc.Width = Unit.Pixel(40);
            tc.Text = drv["AliasName"].ToString();
            tr.Cells.Add(tc);
        }

        tb.Rows.Add(tr);

        int startMonth = Convert.ToInt32(StartMonthList.SelectedValue);
        int endMonth = Convert.ToInt32(EndMonthList.SelectedValue);
        for (int i = startMonth; i <= endMonth; i++)
        {
            TableCell mTc = new TableCell();
            mTc.CssClass = "Td";
            int col = 0;
            mTc.Text = i.ToString();

            col = BindTaskTable(i, col, mTc);
            string strSql = "select * from Base " + GetDepartmentIDChar() + " ";
            DataTable baseDt = SplitPage.ExecuteSQL(strSql).Tables[0];
            foreach (DataRow dr in baseDt.Rows)
            {
                col = BindBaseTable(dv, i, col, mTc, dr);
            }
            col = BindInspectTable(i, col, mTc);
            col = BindCheckTable(i, col, mTc);
            col = BindAwardTable(i, col, mTc);
            mTc.RowSpan = col;
        }
        BindTotalTable();
        BindChart();
    }
    private int BindTaskTable(int mon, int ros, TableCell mTc)
    {
        string departmentid = Yipusoft.Account.BLL.User.GetUser(User.Identity.Name).DepartmentID;
        StringBuilder sql = new StringBuilder();
        sql.AppendLine("DECLARE @SQL nvarchar(4000)");
        sql.AppendLine("SET @SQL = ''");
        sql.AppendLine("SELECT @SQL = @SQL + ', SUM(CASE WHEN UserId = ''' + DepartmentId + ''' THEN Score END) AS D' + DepartmentId FROM Account_Departments WHERE SUBSTRING(DepartmentId,3,2) <> '00' AND SUBSTRING(DepartmentId,5,1) = '0' AND SUBSTRING(DepartmentId,6,1) = '" + departmentid.Substring(5, 1) + "' AND SUBSTRING(DepartmentId,7,1) = '0'  and left(DepartmentID,4)<>'1314' and left(DepartmentID,4)<>'1315'");
        sql.AppendLine("SET @SQL = 'SELECT TaskId ' +@SQL + ' FROM TaskUsers WHERE TaskId IN (SELECT TaskId FROM Tasks WHERE Substring(CreateDepartment,5,1) =''0'' And Substring(CreateDepartment,4,1) =''0'' And Substring(CreateDepartment,6,1)=''" + departmentid.Substring(5, 1) + "'' And YEAR(EndDate) =" + YearList.SelectedValue + " AND MONTH(EndDate)=" + mon + " and CreateDepartment like ''1300%'') GROUP BY TaskId'");
        sql.AppendLine("EXEC(@SQL)");
        DataView dv = SplitPage.ExecuteSQL(sql.ToString()).Tables[0].DefaultView;
        TableCell tc;
        string title = "";
        foreach (DataRowView drv in dv)
        {
            TableRow tr = new TableRow();
            tr.CssClass = "grid_item";
            tr.HorizontalAlign = HorizontalAlign.Right;

            if (ros == 0)
            {
                tr.Cells.Add(mTc);
            }
            ros++;

            //多选框
            tc = new TableCell();
            tc.CssClass = "Td";
            tc.HorizontalAlign = HorizontalAlign.Center;
            CheckBox chk = new CheckBox();
            chk.ID = "Taskchk" + drv[0].ToString();
            tc.Controls.Add(chk);
            Label lbText = new Label();
            lbText.ID = "lbTextr" + drv[0].ToString();
            lbText.Text = "Task";
            lbText.Visible = false;
            tc.Controls.Add(lbText);
            Label lbID = new Label();
            lbID.Text = drv[0].ToString();
            lbID.Visible = false;
            tc.Controls.Add(lbID);
            tr.Cells.Add(tc);

            tc = new TableCell();
            tc.CssClass = "Td";
            tc.HorizontalAlign = HorizontalAlign.Left;
            title = row.ToString() + "、" + Task.GetTask((int)drv[0]).Title;
            if (title.Length > tlength)
                tc.Text = title.Substring(0, tlength) + "...";
            else
                tc.Text = title;
            tr.Cells.Add(tc);


            for (int i = 1; i <= dcount; i++)
            {
                tc = new TableCell();
                tc.CssClass = "Td";
                tc.HorizontalAlign = HorizontalAlign.Right;
                tc.Width = Unit.Pixel(40);
                string RootId = drv[0].ToString();

                string UserId = dv.Table.Columns[i].ColumnName.Replace("D", "");
                if (drv[i] != DBNull.Value)
                {
                    string Text = decimal.Parse(drv[i].ToString()).ToString("0");
                    tc.Text = "<a href=TaskView.aspx?RootId=" + RootId + "&UserId=" + UserId + ">" + Text + "</a>";
                }
                else
                {

                    tc.Text = "<a href=TaskView.aspx?RootId=" + RootId + "&UserId=" + UserId + ">未审核</a>";
                }
                tr.Cells.Add(tc);
                if (drv[i] != DBNull.Value)
                {
                    total[i - 1] += (decimal)drv[i];
                }

            }
            tb.Rows.Add(tr);
            row++;
        }

        return ros;
    }


    private int BindBaseTable(DataView dvv, int mon, int ros, TableCell mTc, DataRow dr)
    {

        StringBuilder sql = new StringBuilder();
        sql.AppendLine("DECLARE @SQL nvarchar(4000)");
        sql.AppendLine("SET @SQL = ''");
        sql.AppendLine("SELECT @SQL = @SQL + ', SUM(CASE WHEN LEFT(b.DepartmentId,4) = ''' + LEFT(DepartmentId,4) + ''' THEN b.ItemScore END) AS D' + LEFT(DepartmentId,4) + '000' FROM Account_Departments WHERE SUBSTRING(DepartmentId,3,2) <> '00' AND SUBSTRING(DepartmentId,5,3) = '000'  and left(DepartmentID,4)<>'1314' and left(DepartmentID,4)<>'1315'");

        sql.AppendLine("SET @SQL = 'SELECT a.BaseID, a.BaseName' + @SQL + ' FROM Base a LEFT JOIN BaseItems b ON a.BaseID = b.BaseID WHERE (b.YearValue=" + YearList.SelectedValue + " and b.MonthValue=" + mon + ") and a.BaseID=" + dr["BaseID"] + " and a.flag=0  GROUP BY a.BaseID, a.BaseName'");
        sql.AppendLine("EXEC(@SQL)");
        DataRow drv = null;
        DataTable dt = SplitPage.ExecuteSQL(sql.ToString()).Tables[0];
        if (dt.Rows.Count > 0)
        {
            drv = dt.Rows[0];
        }
        TableRow tr = new TableRow();
        // tr.CssClass = "grid_item";
        tr.HorizontalAlign = HorizontalAlign.Right;
        TableCell tc = new TableCell();

        if (ros == 0)
        {
            tr.Cells.Add(mTc);
        }
        ros++;

        //多选框              
        tc.CssClass = "Td";
        tc.HorizontalAlign = HorizontalAlign.Center;
        CheckBox check = new CheckBox();
        check.ID = "checkb" + ros.ToString() + mon.ToString() + dr["BaseID"].ToString();
        tc.Controls.Add(check);

        Label lbText = new Label();
        lbText.ID = "lbText" + ros.ToString() + mon.ToString() + dr["BaseID"].ToString();
        lbText.Text = "Base";
        lbText.Visible = false;
        tc.Controls.Add(lbText);

        Label lbID = new Label();
        lbID.ID = "lbID" + ros.ToString() + mon.ToString() + dr["BaseID"].ToString();
        lbID.Text = dr["BaseID"].ToString();
        lbID.Visible = false;
        tc.Controls.Add(lbID);


        Label lbMon = new Label();
        lbMon.ID = "lbMon" + ros.ToString() + mon.ToString() + dr["BaseID"].ToString();
        lbMon.Text = mon.ToString();
        lbMon.Visible = false;
        tc.Controls.Add(lbMon);

        tr.Cells.Add(tc);

        tc = new TableCell();
        tc.CssClass = "Td";
        tc.HorizontalAlign = HorizontalAlign.Left;
        tc.Text = row.ToString() + "、" + Base.GetBase(Convert.ToInt32(dr["BaseID"])).BaseName;
        tr.Cells.Add(tc);

        for (int i = 2; i <= dcount + 1; i++)
        {


            tc = new TableCell();
            tc.CssClass = "Td";
            tc.BackColor = System.Drawing.Color.White;
            tc.HorizontalAlign = HorizontalAlign.Right;
            if (drv != null && drv[i] != DBNull.Value)
                temp = Base.GetBase(Convert.ToInt32(dr["BaseID"])).TotalScore - (decimal)drv[i];
            else
                temp = Base.GetBase(Convert.ToInt32(dr["BaseID"])).TotalScore;
            total[i - 2] += temp;
            tc.Text = "";
            tc.Text = "<a href=BaseView2.aspx?BaseId=" + dr["BaseID"] + "&DepartmentId=" + dt.Columns[i].ColumnName.Replace("D", "") + "&Year=" + YearList.SelectedValue + "&month=" + mon + "&EndMonth=" + mon + ">" + temp.ToString("0") + "</a>";
            tr.Cells.Add(tc);
        }
        tb.Rows.Add(tr);
        row++;
        return ros;

    }

    private int BindInspectTable(int mon, int ros, TableCell mTc)
    {
        string departmentid = Yipusoft.Account.BLL.User.GetUser(User.Identity.Name).DepartmentID;
        StringBuilder sql = new StringBuilder();
        sql.AppendLine("DECLARE @SQL nvarchar(4000)");
        sql.AppendLine("SET @SQL = ''");
        sql.AppendLine("SELECT @SQL = @SQL + ', SUM(CASE WHEN LEFT(b.DepartmentId,4) = ''' + LEFT(DepartmentId,4) + ''' THEN b.ItemScore END) AS D' + LEFT(DepartmentId,4) + '0" + departmentid.Substring(5, 1) + "0' FROM Account_Departments WHERE SUBSTRING(DepartmentId,3,2) <> '00' AND SUBSTRING(DepartmentId,5,3) = '000'  and left(DepartmentID,4)<>'1314' and left(DepartmentID,4)<>'1315'");
        sql.AppendLine("SET @SQL = 'SELECT a.InspectID, a.InspectName' + @SQL + ' FROM Inspects a LEFT JOIN InspectItems b ON a.InspectID = b.InspectID WHERE YEAR(a.InspectDate)=" + YearList.SelectedValue + " AND MONTH(a.InspectDate)=" + mon
 + " " + GetDepartmentID3() + " and a.flag=0 GROUP BY a.InspectID, a.InspectName, a.InspectDate ORDER BY a.InspectDate'");
        sql.AppendLine("EXEC(@SQL)");
        DataView dv = SplitPage.ExecuteSQL(sql.ToString()).Tables[0].DefaultView;
        TableRow tr;
        TableCell tc;
        string title = "";

        foreach (DataRowView drv in dv)
        {
            tr = new TableRow();
            tr.CssClass = "grid_item";

            if (ros == 0)
            {
                tr.Cells.Add(mTc);
            }
            ros++;

            //多选框
            tc = new TableCell();
            tc.CssClass = "Td";
            tc.HorizontalAlign = HorizontalAlign.Center;
            CheckBox ckb = new CheckBox();
            ckb.ID = "Ickb" + drv[0].ToString();
            tc.Controls.Add(new CheckBox());

            Label lbText = new Label();
            lbText.Text = "Inspect";
            lbText.Visible = false;
            tc.Controls.Add(lbText);
            Label lbID = new Label();
            lbID.Text = drv[0].ToString();
            lbID.Visible = false;
            tc.Controls.Add(lbID);

            tr.Cells.Add(tc);



            tc = new TableCell();
            tc.CssClass = "Td";
            tc.BackColor = System.Drawing.Color.White;
            title = row.ToString() + "、" + Inspect.GetInspect((int)drv[0]).InspectName;
            if (title.Length > tlength)
                tc.Text = title.Substring(0, tlength) + "...";
            else
                tc.Text = title;
            tr.Cells.Add(tc);
            for (int i = 2; i <= dcount + 1; i++)
            {
                tc = new TableCell();
                tc.CssClass = "Td";
                tc.BackColor = System.Drawing.Color.White;
                tc.HorizontalAlign = HorizontalAlign.Right;
                if (drv[i] != DBNull.Value)
                    temp = Inspect.GetInspect((int)drv[0]).TotalScore - (decimal)drv[i];
                else
                    temp = Inspect.GetInspect((int)drv[0]).TotalScore;
                total[i - 2] += temp;
                tc.Text = "<a href=InspectView.aspx?InspectId=" + drv[0].ToString() + "&DepartmentId=" + dv.Table.Columns[i].ColumnName.Replace("D", "") + ">" + temp.ToString("0") + "</a>";
                tr.Cells.Add(tc);
            }
            tb.Rows.Add(tr);
            row++;
        }
        return ros;
    }
    private int BindCheckTable(int mon, int ros, TableCell mTc)
    {
        string departmentid = Yipusoft.Account.BLL.User.GetUser(User.Identity.Name).DepartmentID;
        StringBuilder sql = new StringBuilder();
        sql.AppendLine("DECLARE @SQL nvarchar(4000)");
        sql.AppendLine("SET @SQL = ''");
        sql.AppendLine("SELECT @SQL = @SQL + ', SUM(CASE WHEN LEFT(b.DepartmentId,4) = ''' + LEFT(DepartmentId,4) + ''' THEN b.ItemScore END) AS D' + LEFT(DepartmentId,4) + '0" + departmentid.Substring(5, 1) + "0' FROM Account_Departments WHERE SUBSTRING(DepartmentId,3,2) <> '00' AND SUBSTRING(DepartmentId,5,3) = '000'  and left(DepartmentID,4)<>'1314' and left(DepartmentID,4)<>'1315'");
        sql.AppendLine("SET @SQL = 'SELECT a.CheckID, a.CheckName' + @SQL + ' FROM Checks a LEFT JOIN CheckItems b ON a.CheckID = b.CheckID WHERE YEAR(a.CheckDate)=" + YearList.SelectedValue + " AND MONTH(a.CheckDate)=" + mon + " " + GetDepartmentID3() + " and a.flag=0 GROUP BY a.CheckID, a.CheckName, a.CheckDate ORDER BY a.CheckDate'");
        sql.AppendLine("EXEC(@SQL)");
        DataView dv = SplitPage.ExecuteSQL(sql.ToString()).Tables[0].DefaultView;
        TableRow tr;
        TableCell tc;
        string title = "";
        foreach (DataRowView drv in dv)
        {
            tr = new TableRow();
            tr.CssClass = "grid_item";

            if (ros == 0)
            {
                tr.Cells.Add(mTc);
            }
            ros++;

            //多选框
            tc = new TableCell();
            tc.CssClass = "Td";
            tc.HorizontalAlign = HorizontalAlign.Center;
            CheckBox ckb = new CheckBox();
            ckb.ID = "Checkckb" + drv[0].ToString();
            tc.Controls.Add(ckb);

            Label lbText = new Label();
            lbText.Text = "Check";
            lbText.Visible = false;
            tc.Controls.Add(lbText);
            Label lbID = new Label();
            lbID.Text = drv[0].ToString();
            lbID.Visible = false;
            tc.Controls.Add(lbID);

            tr.Cells.Add(tc);




            tc = new TableCell();
            tc.CssClass = "Td";
            tc.BackColor = System.Drawing.Color.White;
            title = row.ToString() + "、" + Check.GetCheck((int)drv[0]).CheckName;
            if (title.Length > tlength)
                tc.Text = title.Substring(0, tlength) + "...";
            else
                tc.Text = title;
            tr.Cells.Add(tc);
            for (int i = 2; i <= dcount + 1; i++)
            {
                tc = new TableCell();
                tc.CssClass = "Td";
                tc.BackColor = System.Drawing.Color.White;
                tc.HorizontalAlign = HorizontalAlign.Right;
                if (drv[i] != DBNull.Value)
                    temp = Check.GetCheck((int)drv[0]).TotalScore - (decimal)drv[i];
                else
                    temp = Check.GetCheck((int)drv[0]).TotalScore;
                total[i - 2] += temp;
                tc.Text = "<a href=CheckView.aspx?CheckId=" + drv[0].ToString() + "&DepartmentId=" + dv.Table.Columns[i].ColumnName.Replace("D", "") + ">" + temp.ToString("0") + "</a>";
                tr.Cells.Add(tc);
            }
            tb.Rows.Add(tr);
            row++;
        }
        return ros;
    }
    private int BindAwardTable(int mon, int ros, TableCell mTc)
    {
        string departmentid = Yipusoft.Account.BLL.User.GetUser(User.Identity.Name).DepartmentID;
        StringBuilder sql = new StringBuilder();
        sql.AppendLine("DECLARE @SQL nvarchar(4000)");
        sql.AppendLine("SET @SQL = ''");
        sql.AppendLine("SELECT @SQL = @SQL + ', SUM(CASE WHEN LEFT(b.DepartmentId,4) = ''' + LEFT(DepartmentId,4) + ''' THEN b.ItemScore END) AS D' + LEFT(DepartmentId,4) + '0" + departmentid.Substring(5, 1) + "0' FROM Account_Departments WHERE SUBSTRING(DepartmentId,3,2) <> '00' AND SUBSTRING(DepartmentId,5,3) = '000'  and left(DepartmentID,4)<>'1314' and left(DepartmentID,4)<>'1315'");
        sql.AppendLine("SET @SQL = 'SELECT a.AwardID, a.AwardName' + @SQL + ' FROM Awards a LEFT JOIN AwardItems b ON a.AwardID = b.AwardID WHERE YEAR(a.AwardDate)=" + YearList.SelectedValue + " AND MONTH(a.AwardDate)=" + mon + " " + GetDepartmentID3() + " and a.flag=0 GROUP BY a.AwardID, a.AwardName, a.AwardDate ORDER BY a.AwardDate'");
        sql.AppendLine("EXEC(@SQL)");
        DataView dv = SplitPage.ExecuteSQL(sql.ToString()).Tables[0].DefaultView;
        TableRow tr;
        TableCell tc;
        string title = "";
        foreach (DataRowView drv in dv)
        {
            tr = new TableRow();
            tr.CssClass = "grid_item";

            if (ros == 0)
            {
                tr.Cells.Add(mTc);
            }
            ros++;

            //多选框
            tc = new TableCell();
            tc.CssClass = "Td";
            tc.HorizontalAlign = HorizontalAlign.Center;
            CheckBox ckb = new CheckBox();
            ckb.ID = "Awardckb" + drv[0].ToString();
            tc.Controls.Add(ckb);

            Label lbText = new Label();
            lbText.Text = "Award";
            lbText.Visible = false;
            tc.Controls.Add(lbText);
            Label lbID = new Label();
            lbID.Text = drv[0].ToString();
            lbID.Visible = false;
            tc.Controls.Add(lbID);



            tr.Cells.Add(tc);
            tc = new TableCell();
            tc.CssClass = "Td";
            tc.BackColor = System.Drawing.Color.White;
            title = row.ToString() + "、" + Award.GetAward((int)drv[0]).AwardName;
            if (title.Length > tlength)
                tc.Text = title.Substring(0, tlength) + "...";
            else
                tc.Text = title;
            tr.Cells.Add(tc);
            for (int i = 2; i <= dcount + 1; i++)
            {
                tc = new TableCell();
                tc.CssClass = "Td";
                tc.BackColor = System.Drawing.Color.White;
                tc.HorizontalAlign = HorizontalAlign.Right;
                if (drv[i] != DBNull.Value)
                    temp = (decimal)drv[i];
                else
                    temp = 0;
                total[i - 2] += temp;
                tc.Text = "<a href=AwardView.aspx?AwardId=" + drv[0].ToString() + "&DepartmentId=" + dv.Table.Columns[i].ColumnName.Replace("D", "") + ">" + temp.ToString("0") + "</a>";
                tr.Cells.Add(tc);
            }
            tb.Rows.Add(tr);
            row++;
        }
        return ros;
    }
    private void BindTotalTable()
    {
        decimal Max = 0;
        TableRow tr = new TableRow();
        tr.ID = Guid.NewGuid() + "tr";
        tr.CssClass = "grid_item";
        TableCell tc = new TableCell();
        TableCell tc1 = new TableCell();
        tc1.CssClass = "Td";
        tc1.ID = Guid.NewGuid() + "td";
        tc1.Text = "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;合计";
        tc1.ColumnSpan = 3;

        tr.Cells.Add(tc1);
        for (int i = 0; i < dcount; i++)
        {
            tc = new TableCell();
            tc.CssClass = "Td";
            tc.ID = Guid.NewGuid() + "td";
            tc.HorizontalAlign = HorizontalAlign.Right;
            tc.Text = total[i].ToString("0");
            tr.Cells.Add(tc);
        }

        tb.Rows.AddAt(tb.Rows.Count, tr);
        tc1.RowSpan = 1;
        int count = tb.Rows.Count;

    }
    private void BindChart()
    {
        decimal Max = 0;
        Max = total.Max();
        List<ResultInfo> results = new List<ResultInfo>();
        ResultInfo result = null;
        DataView dv = SplitPage.ExecuteSQL("SELECT DepartmentID, AliasName FROM Account_Departments WHERE SUBSTRING(DepartmentId,3,2) <> '00' AND SUBSTRING(DepartmentId,5,3) = '000'  and left(DepartmentID,4)<>'1314' and left(DepartmentID,4)<>'1315'").Tables[0].DefaultView;
        for (int i = 0; i < dv.Count; i++)
        {
            result = new ResultInfo();
            result.DepartmentName = dv[i]["AliasName"].ToString();

            if (total[i] > 0)
            {
                result.Score = total[i];
            }
            else
                result.Score = 0;

            results.Add(result);
        }


        Comparer c = new Comparer();
        results.Sort(c);
        UserChart.Titles["Title"].Text = YearList.SelectedItem.Text + "各单位综合得分统计表";
        UserChart.ChartAreas["Default"].AxisX.LabelsAutoFit = false;


        UserChart.Series[0].Points.DataBindXY(results, "DepartmentName", results, "Score");
    }

    protected void ButtonEvaluate_Click(object sender, EventArgs e)
    {
        CreateTable();
    }
    protected void Export_Click(object sender, EventArgs e)
    {
        CreateTable();
        string Task = "";
        string Base = "";
        string Inspect = "";
        string Check = "";
        string Award = "";
        bool has = false;
        int count = tb.Rows.Count;
        foreach (TableRow row in tb.Rows)
        {

            if (row.Cells[1].Controls.Count >= 3)
            {
                CheckBox cbx = row.Cells[1].Controls[0] as CheckBox;
                if (cbx.Checked)
                {
                    Label lbText = row.Cells[1].Controls[1] as Label;
                    Label lbID = row.Cells[1].Controls[2] as Label;
                    if (lbText.Text == "Task")
                    {
                        Task += lbID.Text + ",";
                        if (has == false)
                        {
                            has = true;
                        }
                    }
                    else
                        if (lbText.Text == "Base")
                        {
                            Base += ((Label)row.Cells[1].Controls[3]).Text + ":" + lbID.Text + ",";
                            if (has == false)
                            {
                                has = true;
                            }
                        }
                        else
                            if (lbText.Text == "Inspect")
                            {
                                Inspect += lbID.Text + ",";
                                if (has == false)
                                {
                                    has = true;
                                }
                            }
                            else
                                if (lbText.Text == "Check")
                                {
                                    Check += lbID.Text + ",";
                                    if (has == false)
                                    {
                                        has = true;
                                    }
                                }
                                else
                                    if (lbText.Text == "Award")
                                    {
                                        Award += lbID.Text + ",";
                                        if (has == false)
                                        {
                                            has = true;
                                        }
                                    }
                }
            }

            if (row.Cells[0].Controls.Count >= 3)
            {
                CheckBox cbx = row.Cells[0].Controls[0] as CheckBox;
                if (cbx.Checked)
                {
                    Label lbText = row.Cells[0].Controls[1] as Label;
                    Label lbID = row.Cells[0].Controls[2] as Label;
                    if (lbText.Text == "Task")
                    {
                        Task += lbID.Text + ",";
                        if (has == false)
                        {
                            has = true;
                        }
                    }
                    else
                        if (lbText.Text == "Base")
                        {
                            Base += ((Label)row.Cells[0].Controls[3]).Text + ":" + lbID.Text + ",";
                            if (has == false)
                            {
                                has = true;
                            }
                        }
                        else
                            if (lbText.Text == "Inspect")
                            {
                                Inspect += lbID.Text + ",";
                                if (has == false)
                                {
                                    has = true;
                                }
                            }
                            else
                                if (lbText.Text == "Check")
                                {
                                    Check += lbID.Text + ",";
                                    if (has == false)
                                    {
                                        has = true;
                                    }
                                }
                                else
                                    if (lbText.Text == "Award")
                                    {
                                        Award += lbID.Text + ",";
                                        if (has == false)
                                        {
                                            has = true;
                                        }
                                    }
                }
            }
        }


        if (!has)
        {

            Response.Write("<script>alert('请选择项目！');history.back(0);</script>");
            return;
        }

        string url = "?Year=" + YearList.SelectedValue + "&Sm=" + StartMonthList.SelectedValue + "&Em=" + EndMonthList.SelectedValue + "";
        if (Task != "")
        {

            url += "&Task=" + Task;
        }
        if (Base != "")
        {

            url += "&Base=" + Base;
        }
        if (Inspect != "")
        {

            url += "&Inspect=" + Inspect;
        }
        if (Check != "")
        {

            url += "&Check=" + Check;
        }
        if (Award != "")
        {

            url += "&Award=" + Award;
        }
        Response.Redirect("SomeDetail.aspx" + url);
    }
}